//
//  HXImagesViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/26.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "HXImagesViewController.h"

@interface HXImagesViewController ()<UIScrollViewDelegate>{
    UIPageControl *pagectl;
    UIScrollView *sclView;
    UIImageView *imageView;
}

@end

@implementation HXImagesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"户型"];
    self.view.backgroundColor = [UIColor blackColor];
    
    [self loadUI];
}

- (void)loadUI{
    NSInteger count = self.array.count;
    sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, (ScreenHeight-300)/2, ScreenWidth, 300)];
    sclView.delegate = self;
    sclView.contentSize = CGSizeMake(ScreenWidth*count, 300);
    sclView.showsHorizontalScrollIndicator = NO;
    sclView.pagingEnabled = YES;//分页
    [self.view addSubview:sclView];
    
    for (int i=0; i<self.array.count; i++) {
        imageView = [[UIImageView alloc] init];
        //imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.userInteractionEnabled = YES;//允许用户交互
        //imageView.clipsToBounds = YES;
        NSString *urlStr = [NSString stringWithFormat:@"%@",self.array[i]];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"001"]];
        imageView.frame = CGRectMake(i*ScreenWidth, 0, ScreenWidth, 300);
        
        [sclView addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBack)];
        [imageView addGestureRecognizer:tap];
    }
    
    pagectl = [[UIPageControl alloc] init];
    pagectl.numberOfPages = count;
    pagectl.currentPage = 0;
    pagectl.pageIndicatorTintColor = [UIColor grayColor];
    pagectl.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.view addSubview:pagectl];
    [pagectl makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.bottom.equalTo(self.view.bottom).offset(-10);
        make.width.equalTo(100);
        make.height.equalTo(30);
    }];
    
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger page = (int)(scrollView.contentOffset.x/scrollView.bounds.size.width + 0.5);
    pagectl.currentPage = page;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return imageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGRect frame= imageView.frame;
    frame.origin.y = (sclView.frame.size.height - imageView.frame.size.height)>0? (sclView.frame.size.height - imageView.frame.size.height)*0.5:0;
    
    frame.origin.x = (sclView.frame.size.width - imageView.frame.size.width)>0? (sclView.frame.size.width - imageView.frame.size.width)*0.5:0;
    
    imageView.frame = frame;
    
    sclView.contentSize = CGSizeMake(imageView.frame.size.width+30, imageView.frame.size.height+30);
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
